package sy.service;

import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import sy.po.Resource;
import sy.po.User;

/**
 * 资源服务类
 * 
 * @author 孙宇
 *
 */
@IocBean(name = "resourceService")
public class ResourceService {

	@Inject("refer:dao")
	private Dao dao;

	/**
	 * 通过用户获得当前用户可以访问的资源集合
	 * 
	 * @param user
	 * @return
	 */
	public List<Resource> getResourceListByUser(User user) {
		List<Resource> resources = dao.query(Resource.class, Cnd.where("pid", "is", null).asc("seq"));
		this.fetchLinks(resources);
		return resources;
	}

	private void fetchLinks(List<Resource> resources) {
		if (resources != null && resources.size() > 0) {
			dao.fetchLinks(resources, null, Cnd.orderBy().asc("seq"));
			for (Resource r : resources) {
				if (r.getResources() != null && r.getResources().size() > 0) {
					this.fetchLinks(r.getResources());
				}
			}
		}
	}

}
